<!--
  Created by IntelliJ IDEA.
  User: Dai
  Date: 2018/4/24
  Time: 14:57
  Remark: 工程配置 -  对象管理 -  项目管理 - 新增项目
  To change this template use File | Settings | File Templates.
-->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">新增项目</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/project/objectManage/save_project">
    <div class="modal-body">
        <input type="hidden" name="id" th:value="${projectManage==null?'':projectManage.id}">
        <div class="form-group">
            <label class="col-lg-3 control-label">项目名称</label>
            <div class="col-lg-6">
                <input type="text" th:value="${projectManage==null?'':projectManage.name}" class="form-control"
                       name="name"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">IP地址</label>
            <div class="col-lg-6">
                <input type="text" th:value="${projectManage==null?'':projectManage.ip}" class="form-control"
                       name="ip"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">上传周期</label>
            <div class="col-lg-6">
                <input type="text" th:value="${projectManage==null?'':projectManage.uploadCycle}" class="form-control"
                       name="uploadCycle"/>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>
<script>
    initCheck();
    //初始化表单验证
    function initCheck() {
        $('#defaultForm')
            .bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    name: {
                        message: '不是有效的名称',
                        validators: {
                            notEmpty: {
                                message: '名称不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 30,
                                message: '名称不能超过30个字符'
                            }
                        }
                    },
                    ip: {
                        validators: {
                            notEmpty: {
                                message: 'ip不能为空'
                            },
                            regexp: {
                                regexp: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
                                message: '请输入正确的ip地址'
                            }
                        }
                    },
                    uploadCycle: {
                        validators: {
                            notEmpty: {
                                message: '上传周期不能为空'
                            },
                            regexp: {
                                regexp: /^[0-9]+$/,
                                message: '必须为数字'
                            }
                        }
                    }
                }
            })
            .on('success.form.bv', function (e) {
                // 阻止默认提交事件
                e.preventDefault();
                // 获取form表单
                var $form = $(e.target);
                // Get the BootstrapValidator instance
                var bv = $form.data('bootstrapValidator');
                console.log($('#defaultForm').serialize());
                $.post($form.attr('action'), $form.serialize(), function(result) {
                    console.log(result);
                    var data = eval('('+result+')');
                    if (data.type == "alert") {
                        alert(data.message);
                    }
                    $('#defaultForm').prev().find("button").click();
                    $("#dataTable").bootstrapTable("selectPage",1);
                }, 'text');
            });
    }
</script>